package school;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SearchServlet
 */
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JDBCOptionDAO jdbcOption = new JDBCOptionDAO();

		String key_word = request.getParameter("key_word");
		String date_start = request.getParameter("date_start");
		String location = request.getParameter("list_locs");
		Set<Option> listLocsBySearch = jdbcOption.getListBySearch(key_word, date_start, location);
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Welcome - Your OnLine School !</title>");
		out.println("</head>");
		out.println("<body>");
		
		out.println("<form name=\"myResults\" method=\"POST\" action=\"RegSuccessServlet\">");
		out.println("<h3>Here are the Courses you search</h3>");
		out.println("<table col=\"5\"><tr>");
		out.println("<th></th><th>Code</th><th>Title</th><th>Sessions</th><th>Location</th><th></th></tr>");
		
		for(Option o : listLocsBySearch) {
			out.println("<tr>");
			out.println("<td><input type=\"checkbox\" name=\"check_option\" value=\"" + o.getSession_id() + "\" ></td>");
			out.println("<td>" + o.getCode() + "</td>");
			out.println("<td>" + o.getTitle() + "</td>");
			out.println("<td>" + o.getDate_start() + " to " + o.getDatae_end() + "</td>");
			out.println("<td>" + o.getLocation() + "</td>");
			out.println("<td></td></tr>");
		}

		out.println("</table>");

		out.println("<input type=\"submit\" value=\"Register\"/><br/>");
		out.println("</form>");
		
		out.println("<form name=\"return\" method=\"GET\" action=\"HomeServlet\">");
		out.println("<input type=\"submit\" value=\"Return\"/>");
		out.println("</form>");
		out.println("</body>");
		out.println("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
